﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;

namespace MShop.Core.DataAccess
{
    public class CartItemDao : CommonDao<CartItem>, ICartItemDao
    {
        public CartItemDao(IObjectContextProvider provider)
            : base(provider)
        {
        }


        public void UpdateCartItem(string sessionId,Guid userId)
        {
            var q = from item in GetAll() where item.SessionId == sessionId select item;
            var q2 = from item in GetAll() where item.UserId == userId select item;
            List<CartItem> mylist = new List<CartItem>();
            foreach (var item in q)
            {
                if (q2.Any(x => x.ProductId == item.ProductId))
                {
                    CartItem cartitem = q2.First(x => x.ProductId == item.ProductId);
                    item.Quanitiy += cartitem.Quanitiy;
                    item.UserId = userId;
                    item.SessionId = null;
                    item.IsAnomymous = false;
                    mylist.Add(cartitem);
                }
                else
                {
                    item.UserId = userId;
                    item.SessionId = null;
                    item.IsAnomymous = false;
                }
            }
            Delete(mylist);
           
        }


        public override void Save(CartItem entity)
        {
            if (entity.UserId.HasValue)
            {
                if (entity.IsEmpty)
                {
                    var cart = (from item in GetAll()
                                where item.UserId == entity.UserId.Value && item.ProductId == entity.ProductId
                                select item).FirstOrDefault();
                    if (cart == null)
                    {
                        base.Save(entity);
                    }
                    else
                    {
                        //update
                        cart.Quanitiy += entity.Quanitiy;
                    }
                }
                else
                {
                    var cart = (from item in GetAll()
                                where item.UserId == entity.UserId.Value && item.ProductId == entity.ProductId && item.SkuId == entity.SkuId
                                select item).FirstOrDefault();
                    if (cart == null)
                    {
                        base.Save(entity);
                    }
                    else
                    {
                        //update
                        cart.Quanitiy += entity.Quanitiy;
                    }
                }
            }
            else
            {
                if (entity.IsEmpty)
                {
                    var cart = (from item in GetAll()
                                where item.SessionId == entity.SessionId && item.ProductId == entity.ProductId
                                select item).FirstOrDefault();
                    if (cart == null)
                    {
                        base.Save(entity);
                    }
                    else
                    {
                        //update
                        cart.Quanitiy += entity.Quanitiy;
                    }
                }
                else
                {
                    var cart = (from item in GetAll()
                                where item.SessionId == entity.SessionId && item.ProductId == entity.ProductId && item.SkuId == entity.SkuId
                                select item).FirstOrDefault();
                    if (cart == null)
                    {
                        base.Save(entity);
                    }
                    else
                    {
                        //update
                        cart.Quanitiy += entity.Quanitiy;
                    }
                }
            }           
        }


        #region ICartItemDao Members


        public IQueryable<CartItem> GetCurrentCartItems(bool isAuthenticated, IShopContext shopContext,out decimal totalFee)
        {
            IQueryable<CartItem> q = null;
            if (isAuthenticated)
            {
                q= Find(x => x.UserId == shopContext.CurrentUser.UserId);
            }
            else
            {
               q= Find(x => x.SessionId == shopContext.SessionId);
            }
            totalFee = 0;
            foreach (var item in q)
            {
                totalFee += item.TotalPrice;
            }
            return q;
        }

        #endregion

        #region ICartItemDao Members


        public void DeleteCartItems(Guid userid)
        {
            var q = Find(x => x.UserId == userid);
            this.Delete(q);
        }


        public void DeleteCartItems(string sessionid)
        {
            var q = Find(x => x.SessionId == sessionid);
            this.Delete(q);
        }
        #endregion
    }
}